package com.webdesign.action;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.dto.HomeworkInfoDTO;
import com.webdesign.dto.ModifyHomeworkDTO;
import com.webdesign.dto.MyPage;
import com.webdesign.entity.TbTeacher;
import com.webdesign.entity.TbTerm;
import com.webdesign.service.ITbHomeworkService;
import com.webdesign.service.ITbTeacherService;
import com.webdesign.service.ITbTermService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@Controller("AdminCheckHomeAction")
@Scope("prototype")
public class AdminCheckHomeAction {
	
	private String teacherid;
	private String termid;
	private ITbHomeworkService iTbHomeworkService;
	private ITbTermService iTbTermService;
	private ITbTeacherService iTbTeacherService;
	
	private Map root = new HashMap();
	
	private String propath = ServletActionContext.getRequest().getRealPath("");
	
	private String path = ServletActionContext.getRequest().getContextPath();
	
	private Map<String,Object> session = ActionContext.getContext().getSession();
	
	private String username = (String) session.get("username");
	
	private Configuration cfg = new Configuration();
	
	public String checkHomeworks() throws IOException, TemplateException{
		
		List<TbTerm> terms = iTbTermService.showTerms();
		List<TbTeacher> teachers = iTbTeacherService.showPassTeacher();
		List<HomeworkInfoDTO> homeworks =  iTbHomeworkService.checkHomeworks(teacherid, termid);
		
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path", path);
		root.put("username", username);
		root.put("homeworks", homeworks);
		root.put("teachers", teachers);
		root.put("terms", terms);
		Template template = cfg.getTemplate("/admin/homeworksinfo.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		return "success";
	}

	public ITbHomeworkService getiTbHomeworkService() {
		return iTbHomeworkService;
	}

	@Resource(name = "TbHomeworkService")
	public void setiTbHomeworkService(ITbHomeworkService iTbHomeworkService) {
		this.iTbHomeworkService = iTbHomeworkService;
	}

	public ITbTermService getiTbTermService() {
		return iTbTermService;
	}

	@Resource(name = "TbTermService")
	public void setiTbTermService(ITbTermService iTbTermService) {
		this.iTbTermService = iTbTermService;
	}

	public ITbTeacherService getiTbTeacherService() {
		return iTbTeacherService;
	}

	@Resource(name = "TbTeacherService")
	public void setiTbTeacherService(ITbTeacherService iTbTeacherService) {
		this.iTbTeacherService = iTbTeacherService;
	}

	public String getTeacherid() {
		return teacherid;
	}

	public void setTeacherid(String teacherid) {
		this.teacherid = teacherid;
	}

	public String getTermid() {
		return termid;
	}

	public void setTermid(String termid) {
		this.termid = termid;
	}

	public Map getRoot() {
		return root;
	}

	public void setRoot(Map root) {
		this.root = root;
	}
	
}
